#include "main.h"

#define AGE_TIME 43200

Player::Player():PlayedTime(0),PreForce(20),PreAgility(20),PreSavvy(20),Food(100),Water(100),\
				 PreAptitude(20),HP(100),HPMax(100),HPfull(100),Potential(100),Money(100)
{
	TeacherID = PlayerName[0] = ClassID = SexID = Age = Exp = FPplus \
		 = 	Force = Agility = Savvy = Aptitude = Face = 0;
	FP = FPlevel = Foodfull = Waterfull = 0;
	ImageID = 0xFF;
	Bliss = 0x14;
	int i;
	ItemList[0].ItemID = 0x2A;
	ItemList[0].ItemNum = 1;
	for(i = 1;i < 20; ++i)
		ItemList[i].ItemID = ItemList[i].ItemNum = 0;
	for(i = 0;i < 32; ++i)
		SkillList[i].SkillPoint = SkillList[i].SkillID = SkillList[i].SkillLevel = 0;
	for(i = 0;i < 6; ++i)
		SelectedSkill[i] = 0;
	Password[0] = WeaponName[0] = SpouseName[0] = PlayerName[0] = 0;
}

Player::Player(unsigned char *SaveData)
{

	int i;
	ItemList[0].ItemID = 0x2A;
	ItemList[0].ItemNum = 1;
	for(i = 1;i < 20; ++i)
		ItemList[i].ItemID = ItemList[i].ItemNum = 0;
}

BOOL Player::CheckData(void)
{
	if(SkillNumber || Exp)
		return FALSE;
	if(Potential - 0x64)
		return FALSE;
	if(Bliss - 0x14)
		return FALSE;
	return TRUE;
}

//dead
//1-0x7275
void Player::Dead(void)
{
	HPfull = HPMax;
	HP = 1;
	Exp = (Exp * 8) / 10;
	Potential /= 2;
	for(int i = 0; i < 20; ++i)
		ItemList[i].ItemID = ItemList[i].ItemNum = 0;
	for(int i = 0; i < 6; ++i)
		SelectedSkill[i] = 0;
	for(int i = 0; i < SkillNumber; ++i)
		if(SkillList[i].SkillLevel - 1)
			--SkillList[i].SkillLevel;
}

//Base 1-44DD
void Player::CalcFoodFull(void)
{
	Foodfull = (PreForce + 5) * 0xF;
	Waterfull =  (PreForce + 4) * 0xF;
}

//Base 1-860E
void Player::CalcSkillAward(void)
{
	Force = PreForce + (GetSkillLevel(1) / 10);
	Agility = PreAgility + (GetSkillLevel(7) / 10);
	Savvy = PreSavvy + (GetSkillLevel(9) / 10);
	Aptitude = PreAptitude + (GetSkillLevel(0) / 10);
	Face = PreFace + (GetSkillLevel(0xA) / 10);
}

//Base 1-4445
void Player::CalcHPMax(void)
{
	Age = 0xE + PlayedTime / 0xA8C0;
	int a = max(0xE, Age);
	a = min(a, 0x19);
	HPMax = 0x64 + (a - 0xE)* 0x14;
	HPMax += FPlevel / 4;
	int lv;
	if(0x14 <= Age)
		if(0x50 <= (lv = GetSkillLevel(0x16)))
			HPMax += (lv * PreAptitude)/10;
}

unsigned char Player::GetSkillLevel(unsigned char id)
{
	int i = GetSkillLocation(id);
	if(0 > i)
		return 0;
	return SkillList[i].SkillLevel;
}

unsigned char Player::GetItemNumber(unsigned char id)
{
	int i = GetItemLocation(id);
	if(0 > i)
		return 0;
	return ItemList[i].ItemNum;
}

int Player::GetItemLocation(unsigned char id)
{
	if(0 > id)
		return -1;
	for(int i = 0; i < 20 ; ++i)
		if(id == ItemList[i].ItemID && 0 < ItemList[i].ItemNum)
			return i;
	return -1;
}

int Player::GetSkillLocation(unsigned char id)
{
	if(0 > id)
		return -1;
	for(int i = 0; i < SkillNumber ; ++i)
		if(id == SkillList[i].SkillID)
			return i;
	return -1;
}